package com.v2future.sjt.collegecheckingattendance.event;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.event.adapter.EventListAdapter;
import com.v2future.sjt.collegecheckingattendance.event.bean.EventBean;
import com.v2future.sjt.collegecheckingattendance.event.bean.OneEventBean;
import com.v2future.sjt.collegecheckingattendance.event.bean.OneEventTypeBean;
import com.v2future.sjt.collegecheckingattendance.event.bean.SliderBean;
import com.v2future.sjt.collegecheckingattendance.home.HomeActivity;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.DensityUtil;
import com.v2future.sjt.collegecheckingattendance.utils.IntentKey;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;
import com.v2future.sjt.collegecheckingattendance.widget.view.PullToRefreshView;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动列表
 * @author shijianting
 * @time 2016/4/16 9:13
 */
public class EventListActivity extends MainActivity implements BaseSliderView.OnSliderClickListener,
        ViewPagerEx.OnPageChangeListener,
        PullToRefreshView.OnFooterRefreshListener,
        PullToRefreshView.OnHeaderRefreshListener{

    SliderLayout slide;

    ListView eventLv;
    PullToRefreshView pullView;
    EventListAdapter eventListAdapter;

    List<SliderBean> dataList;

    List<OneEventBean> eventList;

    private int totalPage = 0;
    private int currentPage = 0;

    /**
     * 选中的活动类型
     */
    private OneEventTypeBean selectEventType;

    @Override
    public void init() {
        selectEventType = (OneEventTypeBean) getIntent().getSerializableExtra(IntentKey.EVENT_TYPE);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_event_list);
        initTitleBack(null);
        eventLv = (ListView) findViewById(R.id.lv_event);
        pullView = (PullToRefreshView) findViewById(R.id.pullview1);
        pullView.setOnFooterRefreshListener(this);
        pullView.setOnHeaderRefreshListener(this);
        addSlider();
    }

    @Override
    public void initAfter() {
        setTitleText(selectEventType.getSubjectName());
        setRightText(getString(R.string.release), releaseListener);
        showTitleLoading();
        doGetEventList(currentPage);
    }

    @Override
    public void onNewIntent(Intent intent){
        boolean refreshTag = intent.getBooleanExtra(HomeActivity.INTENT_TAG_REFRESH, false);
        //刷新活动列表
        if (refreshTag) {
            currentPage = 1;
            doGetEventList(currentPage);
        }
    }

    /**
     * 添加轮播
     */
    private void addSlider(){
        slide = new SliderLayout(this);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(this, 150));
        slide.setLayoutParams(params);
        slide.setPresetTransformer(SliderLayout.Transformer.Accordion);
        slide.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        slide.setCustomAnimation(new DescriptionAnimation());
        slide.setDuration(4000);
        dataList = getSliderDatas();
        for (SliderBean bean:
                dataList) {
            TextSliderView tsv = new TextSliderView(this);
            tsv
//                    .description(bean.getDescription())
                    .image(bean.getImageUrl())
                    .setScaleType(BaseSliderView.ScaleType.Fit)
                    .setOnSliderClickListener(this);
            Bundle bundle = new Bundle();
            bundle.putString("name", bean.getDescription());
            tsv.bundle(bundle);
            slide.addSlider(tsv);
        }
        slide.addOnPageChangeListener(this);
//        eventLv.addHeaderView(slide);
    }

    public ArrayList<SliderBean> getSliderDatas(){
        ArrayList<SliderBean> result = new ArrayList<SliderBean>();
        SliderBean bean;
        bean = new SliderBean();
        bean.setImageUrl("http://pics.sc.chinaz.com/files/pic/pic9/201603/apic19251.jpg");
        bean.setDescription("美女1");
        result.add(bean);
//        bean = new SliderBean();
//        bean.setImageUrl("http://pics.sc.chinaz.com/files/pic/pic9/201402/apic512.jpg");
//        bean.setDescription("美女2");
//        result.add(bean);
//        bean = new SliderBean();
//        bean.setImageUrl("http://pics.sc.chinaz.com/files/pic/pic9/201208/xpic6595.jpg");
//        bean.setDescription("美女3");
//        result.add(bean);
        return result;
    }

    /**
     * 活动点击事件
     */
    private AdapterView.OnItemClickListener eventClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int headViewsCount = eventLv.getHeaderViewsCount();
            position = position - headViewsCount;
            OneEventBean currentEventBean = eventList.get(position);
            Intent intent = new Intent(EventListActivity.this, EventDetailActivity.class);
            intent.putExtra(EventDetailActivity.DATA_EVENT, currentEventBean);
            startActivity(intent);
        }
    };

    /**
     * 获取活动列表
     */
    public void doGetEventList(final int page){
        BaseRequestParams params = new BaseRequestParams(this, this.formatUrl(Constants.EVENT_LIST));
        if (null != selectEventType && !TextUtils.isEmpty(selectEventType.getSubjectId())) {
            params.addQueryStringParameter("subjectId", selectEventType.getSubjectId());
        }
        if (page > 1) {
            params.addQueryStringParameter("page", page+"");
        }
        NetUtils.doGetRequest(this, params, new NetTaskListener<EventBean>() {

            @Override
            public void onSuccess(EventBean result) {
                try {
                    currentPage = Integer.parseInt(result.getPage());
                    totalPage = Integer.parseInt(result.getTotalPage());
                }catch (Exception ne) {
                    ne.printStackTrace();
                }
                if (currentPage <= 1) {
                    eventList = result.getData();
                    eventListAdapter = new EventListAdapter(EventListActivity.this, eventList);
                    eventLv.setAdapter(eventListAdapter);
                    eventLv.setOnItemClickListener(eventClickListener);
                    if (eventList.size() == 0) {
                        tip("暂无活动");
                    }
                }
                else {
                    eventList.addAll(result.getData());
                    eventListAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError() {
                tip(getString(R.string.unknown_error));
            }

            @Override
            public void onInterError(EventBean result) {
                if (!TextUtils.isEmpty(result.getFailReason())) {
                    tip(result.getFailReason());
                }
                else {
                    tip(getString(R.string.have_an_error));
                }
            }

            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            @Override
            public void onCancelled() {
                cancelTitleLoading();
                pullView.onHeaderRefreshComplete();
                pullView.onFooterRefreshComplete();
            }

            @Override
            public void onFinished() {
                cancelTitleLoading();
                pullView.onHeaderRefreshComplete();
                pullView.onFooterRefreshComplete();
            }

            @Override
            public void onSuccessCache(EventBean result) {

            }
        });
    }

    @Override
    public void onSliderClick(BaseSliderView slider) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        if (totalPage == currentPage) {
            pullView.onFooterRefreshComplete();
            this.tip("已到尾页");
            return;
        }
        currentPage++;
        doGetEventList(currentPage);
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        currentPage = 1;
        doGetEventList(currentPage);
    }

    /**
     * 发布点击事件
     */
    private View.OnClickListener releaseListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(EventListActivity.this, ReleaseEventActivity.class);
            intent.putExtra(IntentKey.EVENT_TYPE, selectEventType);
            startActivity(intent);
        }
    };
}
